////
////  InteractiveWebVC.swift
////  rrlWallet
////
////  Created by gordon on 2018/10/31.
////  Copyright © 2018 rrl360. All rights reserved.
////
//
//import UIKit
//import WebKit
//import WebViewJavascriptBridge
//
//extension InteractiveWebVC: Routable {
//    var umName: String {
//        return "H5活动页面"
//    }
//
//    static func initWithParams(params: RouterParameter?) -> UIViewController? {
//        guard let param = params?["webParam"] as? InteractiveWebParam else {
//            d_print("error init \(self) by illegal params")
//            return nil
//        }
//        return InteractiveWebVC(webParam: param)
//    }
//}
//
//final class InteractiveWebVC: LYBaseVC {
//
//    private(set) var webView: LYWKWebView!
//    var bridge: WKWebViewJavascriptBridge!
//    private var progressView: UIProgressView!
//    private(set) var webParam: InteractiveWebParam
//    /// 登录成功后，把token传递给H5
//    var isNeedCallUserToken = false
//    private var backListCount = 0
//
//    lazy var userNet: LYNetProvider<RRUserNetConfig> = {
//        return LYNetProvider<RRUserNetConfig>()
//    }()
//    lazy var newApp: LYNetProvider<RRNewNetConfig> = {
//        return LYNetProvider<RRNewNetConfig>()
//    }()
//
//    init(webParam: InteractiveWebParam) {
//        self.webParam = webParam
//        super.init(nibName: nil, bundle: nil)
//    }
//
//    required init?(coder aDecoder: NSCoder) {
//        fatalError("init(coder:) has not been implemented")
//    }
//
//    // MARK: view cycle
//    override func viewDidLoad() {
//        super.viewDidLoad()
//        self.p_initSubviews()
//    }
//
//    override func viewWillAppear(_ animated: Bool) {
//        super.viewWillAppear(animated)
//        if isNeedCallUserToken,
//           RRLocalMng.isLogin {
//            p_setLoginToken()
//        }
//    }
//
//    override func viewWillDisappear(_ animated: Bool) {
//        super.viewWillDisappear(animated)
//        view.endEditing(true)
//    }
//
//    deinit {
//        webView.removeObserver(self, forKeyPath: "canGoBack")
//        webView.removeObserver(self, forKeyPath: "estimatedProgress")
//    }
//
//    override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey: Any]?, context: UnsafeMutableRawPointer?) {
//        if keyPath == "canGoBack" {
//            if webView.canGoBack {
//                self.fd_interactivePopDisabled = true
//            } else {
//                self.fd_interactivePopDisabled = false
//            }
////            let backCount = webView.backForwardList.backList.count
////            if backCount < backListCount {
////
////            }
////            backListCount = backCount
//        } else if keyPath == "estimatedProgress" {
//            progressView.alpha = CGFloat(1.0 - webView.estimatedProgress) + 0.2
//            let progerss = max(Float(webView.estimatedProgress), 0.1)
//            progressView.setProgress(progerss, animated: true)
//            if webView.estimatedProgress >= 1.0 {
//                progressView.alpha = 0.0
//                progressView.setProgress(0.0, animated: false)
//            }
//        } else {
//            super.observeValue(forKeyPath: keyPath, of: object, change: change, context: context)
//        }
//    }
//}
//
//// MARK: - ********* WKNavigationDelegate
//extension InteractiveWebVC: WKNavigationDelegate {
//
//    func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
//        d_print("=======")
//    }
//
//    func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
//        d_print("==== error: \(error)")
//    }
//
//    func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: Error) {
//        d_print("==== error: \(error)")
//    }
//
//    func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
//        if (navigationAction.targetFrame == nil) {
//            webView.load(navigationAction.request)
//        }
//        guard let policy = WKNavigationActionPolicy(rawValue: WKNavigationActionPolicy.allow.rawValue + 2)
//            else {
//            decisionHandler(.allow);
//            return
//        }
//        decisionHandler(policy)
//    }
//}
//
//// MARK: - ********* UI init
//extension InteractiveWebVC {
//    fileprivate func p_initSubviews() {
//        self.navigationItem.title = webParam.title
//        _ = setNavLeft()
//
//        webView = LYWKWebView(frame: CGRect.init(x: 0, y: kNavBottom(), width: kScreenWid(), height: kScreenHei() - kNavBottom() - kBtmSafeHei()))
//        
//        //MARK: warning 思维导图页面隐藏导航栏
//        if webParam.urlStr.contains("mind-map") {
//            self.fd_prefersNavigationBarHidden = true
//            self.navigationController?.isNavigationBarHidden = true
//            webView.frame = CGRect.init(x: 0, y: 0, width: kScreenWid(), height: kScreenHei() - kBtmSafeHei())
//            
//            if #available(iOS 11.0, *) {
//                webView.scrollView.contentInsetAdjustmentBehavior = .never
//            }
//            
//        }
//        
//        webView.navigationDelegate = self
//        webView.uiDelegate = self
////        webView.scrollView.delegate = self
////        webView.allowsBackForwardNavigationGestures = true
//        view.addSubview(webView)
//        /// 进度条
//        progressView = UIProgressView(progressViewStyle: .default)
//        progressView.frame = CGRect(x: 0, y: 0, width: webView.width, height: 4)
//        progressView.trackTintColor = UIColor.clear
//        progressView.progressTintColor = kThemeColor()
//        progressView.isHidden = false
//        webView.addSubview(progressView)
//
//        
//        /// 监听支持KVO的属性
//        webView.addObserver(self, forKeyPath: "estimatedProgress", options: .new, context: nil)
//        webView.addObserver(self, forKeyPath: "canGoBack", options: .new, context: nil)
//
//        /// 加载URL
//        let urlStr = webParam.urlStr.ly.urlReverify
//        
//        
//        
//        
//        if let requestUrl = URL(string: urlStr) {
//            let request = URLRequest(url: requestUrl)
//            webView.load(request)
//        } else {
//            
//            let urlnew = urlStr.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
//            if let requestUrl = URL(string: urlnew!) {
//                let request = URLRequest(url: requestUrl)
//                webView.load(request)
//            } else {
//                return
//            }
//            
//            //return
//        }
//        
//        progressView.setProgress(0.05, animated: true)
//
//        /// 设置JS交互
//        p_setJSInteractive()
//    }
//}
//
//// MARK: - WKUIDelegate 不实现该代理方法 网页内调用弹窗时会抛出异常,导致程序崩溃
//extension InteractiveWebVC: WKUIDelegate {
//
//    // 获取js 里面的提示
//    func webView(_ webView: WKWebView, runJavaScriptAlertPanelWithMessage message: String, initiatedByFrame frame: WKFrameInfo, completionHandler: @escaping () -> Void) {
//        let alert = UIAlertController(title: "提示", message: message, preferredStyle: .alert)
//        alert.addAction(UIAlertAction(title: "确定", style: .default, handler: { (_) -> Void in
//            completionHandler()
//        }))
//        alert.addAction(UIAlertAction(title: "取消", style: .cancel, handler: { (_) -> Void in
//            completionHandler()
//        }))
//        self.present(alert, animated: true, completion: nil)
//    }
//
//    // js 信息的交流
//    func webView(_ webView: WKWebView, runJavaScriptConfirmPanelWithMessage message: String, initiatedByFrame frame: WKFrameInfo, completionHandler: @escaping (Bool) -> Void) {
//        let alert = UIAlertController(title: "提示", message: message, preferredStyle: .alert)
//        alert.addAction(UIAlertAction(title: "确定", style: .default, handler: { (_) -> Void in
//            completionHandler(true)
//        }))
//        alert.addAction(UIAlertAction(title: "取消", style: .cancel, handler: { (_) -> Void in
//            completionHandler(false)
//        }))
//        self.present(alert, animated: true, completion: nil)
//    }
//
//    // 交互。可输入的文本。
//    func webView(_ webView: WKWebView, runJavaScriptTextInputPanelWithPrompt prompt: String, defaultText: String?, initiatedByFrame frame: WKFrameInfo, completionHandler: @escaping (String?) -> Void) {
//        let alert = UIAlertController(title: prompt, message: defaultText, preferredStyle: .alert)
//        alert.addTextField { (textField: UITextField) -> Void in
//            textField.textColor = UIColor.red
//        }
//        alert.addAction(UIAlertAction(title: "确定", style: .default, handler: { (_) -> Void in
//            completionHandler(alert.textFields![0].text!)
//        }))
//        self.present(alert, animated: true, completion: nil)
//    }
//}
